use std::fs;
use error::AppErr;
use mcu::stm32::{insert_to_db, Mcu};


mod error;
mod store;
mod mcu;
mod config;

const TEST_PATH: &'static str = "D:/STM32CubeMX/db/mcu";
const TEST_FILE: &'static str = "STM32H7B3LIHxQ.xml";

use mcu::stm32;
use server::web_server_run;
mod server;

async fn load_for_file() -> Result<(), AppErr> {

    let dir = fs::read_dir(TEST_PATH)?;
    for file in dir.enumerate() {
        let entry = file.1?;
        if !entry.file_type()?.is_file() {
            continue;
        }
        let file_name = entry.file_name();
        let name = file_name.to_str().unwrap();
        if !name.as_bytes().starts_with(b"STM32") {
            continue;
        }
        println!("save : {}", name);
        let s = fs::read_to_string(entry.path())?;
        let mcu: Mcu = serde_xml_rs::from_str(&s)?;
        insert_to_db(&mcu).await?;
    }   

    Ok(())
}

#[actix_web::main]
async fn main() -> Result<(), AppErr> {
    config::init().await?;
    store::init().await?;
    stm32::init().await?;
 //   load_for_file().await?;

    web_server_run().await?;

    Ok(())
}
